// 获取配置参数
// var baseUrl = "http://10.18.2.47:8081/";
var baseUrl = "http://124.70.148.241:8066/";

var HttpRequest = function(options) {
	var defaults = {
		type: 'get',
		headers: {},
		data: {},
		dataType: 'json',
		async: true,
		cache: false,
		beforeSend: null,
		success: null,
		complete: null
	};
	var o = $.extend({}, defaults, options);
	$.ajax({
		url: baseUrl + o.url,
		type: o.type,
		headers: {
			'Content-Type': o.contentType,
			'X-Access-Token': o.token
		},
		data: o.data,
		dataType: o.dataType,
		async: o.async,
		beforeSend: function() {
			o.beforeSend && o.beforeSend();
		},
		success: function(res) {
			o.success && o.success(res);
			// console.log(JSON.stringify(res));
		},
		error: function(err) {
			let html =
				'<div class="tipWarn"><div class="weui-toptips weui-toptips_warn" style="opacity: 1;display: block;position: initial;width: 100%;border-radius:6px;">登录验证过期</div></div>';
			$('#containerVue').append(html)
			setTimeout(function() {
				$('.tipWarn').remove();
				window.location.href = '../login.html';
			}, 1500)
		},
		complete: function() {
			o.complete && o.complete();
		}
	});
};

var loginHttp = function(options) {
	// 登入页无需携带token
	// 后台如果要求 Content-Type 
	if (options.type == 'post') {
		options.contentType = 'application/x-www-form-urlencoded';
	}
	HttpRequest(options);
}
var ajaxHttp = function(options) {
	if (options.type == 'post') {
		options.contentType = 'application/x-www-form-urlencoded';
	}
	// 每次请求携带token
	options.token = localStorage.getItem('access_token');
	HttpRequest(options);
}
var ajaxHttpJson = function(options) {
	if (options.type == 'post') {
		options.contentType = 'application/json;charset=UTF-8';
	}
	// 每次请求携带token
	options.token = localStorage.getItem('access_token');
	HttpRequest(options);
}
